#include "messages.h"

int Messages :: Create( )
{
	PGresult * res = PQexec( conn, "CREATE TABLE messages ( idmessage serial NOT NULL, iduser integer NOT NULL, time integer NOT NULL, text character varying(32), CONSTRAINT messages_pkey PRIMARY KEY ( idmessage ) );" );
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = DB_NO_RESULT;
	PQclear( res );
	return result;
}

int Messages :: Clean( )
{
	PGresult * res = PQexec( conn, "DROP TABLE messages;" );
	//			cout << PQresultStatus( res ) << endl;
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = DB_NO_RESULT;
	PQclear( res );
	return result;
}

int Messages :: FindRecord( int iduser, int & idsession ) // нужна ли ? -
{
	char st1[ 120 ] = "SELECT idsession FROM sessions WHERE iduser = ";
	char st2[ 5 ];
	char st3[ ] = " LIMIT 1;";

	sprintf( st2, "%i", iduser );
	strncat( st1, st2, 5 );
	strncat( st1, st3, 9 );

	PGresult * res = PQexec( conn, st1 );
	int result = PQresultStatus( res );
	//			cout << "\t\t" << result << endl;
	if ( PQresultStatus( res ) == PGRES_TUPLES_OK )
	{
		if ( PQntuples( res ) )
		{
			result = DB_OK;
			char * endptr;
			idsession = strtol( PQgetvalue( res, 0, 0 ), &endptr, 10 ); // не проверяем.
		}
		else
		{
			result = DB_NO_RECORD;
			idsession = -1;
		}
	}
	else
	{
		result = DB_NO_RESULT;
		idsession = -1;
	}
	//			cout << "idsession " << idsession << "\t" << result << endl;
	PQclear( res ); // ???
	return result;
}

int Messages :: InsRecord( int iduser, char * text ) // +
{
	int result;

	//			cout << "InsRecord( ) idsession = " << idsession << endl;

	char ciduser[ 32 ];
	char ctime[ 32 ];

	sprintf( ciduser, "%i", iduser );
	sprintf( ctime, "%i", time ( NULL ) /*- rand( ) % 1000*/ ); // тестируем GetLastMessage( )


	char * st1;
	ExtraClass :: GetString( st1, "INSERT INTO messages (iduser,time,text) VALUES ('", ciduser,  "','", ctime, "','", text, "');" );

	//			cout << "InsRecord( ) st1 = " << st1 << endl;

	PGresult * res = PQexec( conn, st1 );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = DB_NO_RESULT;
	PQclear( res );
	return result;
}

int Messages :: GetLastMessage( int n, 	int * iduser, int * time_, char * * text ) // +
{
	char * st1;
	char cn[ 32 ];
	sprintf( cn, "%i", n );
	ExtraClass :: GetString( st1, "SELECT iduser,time,text FROM messages ORDER BY time DESC LIMIT ",cn ,";" );

	PGresult * res = PQexec( conn, st1 );
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_TUPLES_OK )
	{
		if ( PQntuples( res ) )
		{
			result = DB_OK;
			char * endptr;

			for( int i = 0; i < n; i++ )
			{
				iduser[ i ] = strtol( PQgetvalue( res, i, 0 ), &endptr, 10 ); // не проверяем.
				time_[ i ] = strtol( PQgetvalue( res, i, 1 ), &endptr, 10 ); // не проверяем.
				strncat( text[ i ], PQgetvalue( res, i, 2 ), 32 );
			}
		}
		else
			result = DB_NO_RECORD;
	}
	else
		result = DB_NO_RESULT;
	PQclear( res ); // ???
	return result;
}
